package cn.dt70.utils;

import cn.dt70.pojo.AqbCommodity;
import cn.dt70.pojo.AqbUser;

import java.util.List;

public class Xiangqing {   //用来储存购物车界面的详细信息
   List<AqbCommodity> aqbCommodity;

   List<AqbUser> aqbUser;

   int num;



    double prices; //总价格

   public List<AqbCommodity> del(int commodityId){ //遍历集合并根据commoditeId来删除对应的物品
       AqbCommodity aqbCommodi =new AqbCommodity();
        for (int i =0;i<aqbCommodity.size();i++){
           aqbCommodi = aqbCommodity.get(i);
           if(commodityId ==aqbCommodi.getCommodityId()){
               aqbCommodity.remove(i);
           }
       }
       return aqbCommodity;
   }

    public List<AqbCommodity> addList(AqbCommodity aqbCommodi){
        AqbCommodity aqbCommodi2 =new AqbCommodity();
        int flag = 0;
        for(int i =0;i<aqbCommodity.size();i++){
            aqbCommodi2 = aqbCommodity.get(i);
            if(aqbCommodi2.getCommodityId() == aqbCommodi.getCommodityId()){  //如果传进来的对象的id和本体id一样，则数量加1
                   int num = aqbCommodi2.getNum();
                   num = num+ aqbCommodi.getNum();
                   aqbCommodi2.setNum(num);
                  flag =1;    //如果flag=1了，那么说明已经向集合中添加数量了。无需在向里面添加对象。
            }
        }
        if(flag ==0){   //flag=0说明没有执行循环，需要向其添加对象。
            aqbCommodity.add(aqbCommodi);
        }
       return aqbCommodity;
    }

    public List<AqbCommodity> gouwuchejia(AqbCommodity aqbCommodi){
        AqbCommodity aqbCommodi2 =new AqbCommodity();
        int flag = 0;
        for(int i =0;i<aqbCommodity.size();i++){
            aqbCommodi2 = aqbCommodity.get(i);
            if(aqbCommodi2.getCommodityId() == aqbCommodi.getCommodityId()){
                int num = aqbCommodi2.getNum();
                num = num+1;
                aqbCommodi2.setNum(num);
                flag =1;    //如果flag=1了，那么说明已经向集合中添加数量了。无需在向里面添加对象。
               }
            }
        return aqbCommodity;
    }

    public List<AqbCommodity> gouwuchejian(AqbCommodity aqbCommodi) {
        AqbCommodity aqbCommodi2 = new AqbCommodity();
        int flag = 0;
        for (int i = 0; i < aqbCommodity.size(); i++) {
            aqbCommodi2 = aqbCommodity.get(i);
            if (aqbCommodi2.getCommodityId() == aqbCommodi.getCommodityId()) {
                int num = aqbCommodi2.getNum();
                num = num - 1;
                aqbCommodi2.setNum(num);
                flag = 1;
            }
        }
            return aqbCommodity;

    }

    public double getAllprices(){
       double newprices = 0;
        AqbCommodity aqbCommodi2 = new AqbCommodity();
        for (int i = 0; i < aqbCommodity.size(); i++) {
            aqbCommodi2 =aqbCommodity.get(i);
           newprices = newprices +aqbCommodi2.getNum()* aqbCommodi2.getNewprice();
        }
       return newprices;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public List<AqbCommodity> getAqbCommodity() {
        return aqbCommodity;
    }

    public void setAqbCommodity(List<AqbCommodity> aqbCommodity) {
        this.aqbCommodity = aqbCommodity;
    }

    public List<AqbUser> getAqbUser() {
        return aqbUser;
    }

    public void setAqbUser(List<AqbUser> aqbUser) {
        this.aqbUser = aqbUser;
    }

    public double getPrices() {
        return prices;
    }

    public void setPrices(double prices) {
        this.prices = prices;
    }
}
